import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Crab here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Crab extends Actor
{
    GreenfootImage image1=new GreenfootImage("crab.png");
    GreenfootImage image2=new GreenfootImage("crab2.png");
    int count =0;
    public void act() 
    {
        move(3);
        keycintrol();
        eat();
        switchImage();
        stop();
    }
    public void switchImage(){
        if(getImage()==image1){
            setImage(image2);
        }
        else{
            setImage(image1);
        }
        
    }
    public void eat()
    {
        if(isTouching(Worm.class)){
           removeTouching(Worm.class);
           count++;
        World w=getWorld();
        w.showText("吃虫数："+count,70,30);
        }
    }
    public void keycintrol(){
        if(Greenfoot.isKeyDown("left")){
            turn(-5);
        }
        if(Greenfoot.isKeyDown("right")){
            turn(5);
        }
        
    
    
    }
    public void stop(){
        if (count==6){
            Greenfoot.stop();
            World w = getWorld();
            GreenfootImage bg = w.getBackground();
            Font font = bg.getFont();
            font = font.deriveFont(150);
            bg.setFont(font);
            bg.setColor(Color.GREEN);
            bg.drawString("成功!",180,280);



}
}
}
